using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
    using AppManagement.Reports;
	#endregion

	public partial class FrmDiemTongHopSV : Office2007Form
	{
        public FrmDiemTongHopSV(string mssv = "")
		{
			InitializeComponent();
            txtMSSV.Text = mssv;
            if (mssv == string.Empty)
            {
                txtMSSV.ReadOnly = false;
            }
		}

        private void FrmQuanLySinhVien_Load(object sender, EventArgs e)
        {
            InitGridView();
        }

        private void txtNamHoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow type number
            if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void txtHocKy_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Only allow type number
            if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back)
            {
                e.Handled = true;
            }
        }

        private void InitGridView()
        {
            DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Năm học",
                DataPropertyName = "NamHoc",
                Name = "NamHoc",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Học kỳ",
                DataPropertyName = "HocKy",
                Name = "HocKy",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Mã HP",
                DataPropertyName = "MaHP",
                Name = "MaHP",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8)) - 30
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Tên HP",
                DataPropertyName = "TenHP",
                Name = "TenHP",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8)) + 90
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemQuaTrinh",
                DataPropertyName = "DiemQuaTrinh",
                Name = "DiemQuaTrinh",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemThi",
                DataPropertyName = "DiemThi",
                Name = "DiemThi",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemTongKet",
                DataPropertyName = "DiemTongKet",
                Name = "DiemTongKet",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8))
            };
            dtgvData.Columns.Add(objColumn);

            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "DiemThiLan2",
                DataPropertyName = "DiemThiLan2",
                Name = "DiemThiLan2",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 8)) - 1
            };
            dtgvData.Columns.Add(objColumn);
        }

        private void btnXemDiem_Click(object sender, EventArgs e)
        {
            if (string.Empty.Equals(txtMSSV.Text.Trim()))
            {
                MessageBoxEx.Show("Bạn chưa nhập MSSV", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var lstDiemHocTap = DiemHocTapRepository.XemDiem(txtMSSV.Text, txtNamHoc.Text, txtHocKy.Text);

            var DataSource = new DataTable();
            DataSource.Columns.Add("NamHoc");
            DataSource.Columns.Add("HocKy");
            DataSource.Columns.Add("MaHP");
            DataSource.Columns.Add("TenHP");
            DataSource.Columns.Add("DiemQuaTrinh");
            DataSource.Columns.Add("DiemThi");
            DataSource.Columns.Add("DiemTongKet");
            DataSource.Columns.Add("DiemThiLan2");

            foreach (var item in lstDiemHocTap)
            {
                DataSource.Rows.Add(item.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.NamHoc, item.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.HocKy, item.ChiTietDangKy.ChiTietKHDT.HocPhan.MaHP, item.ChiTietDangKy.ChiTietKHDT.HocPhan.TenHP, item.DiemQuaTrinh, item.DiemThi, item.DiemTongKet, item.DiemThiLan2);
            }

            DataView objTimeSheetDV = new System.Data.DataView(DataSource);
            objTimeSheetDV.AllowNew = false;
            dtgvData.AutoGenerateColumns = false;
            dtgvData.DataSource = objTimeSheetDV;
            dtgvData.ClearSelection();
        }

        private void btnInBangDiem_Click(object sender, EventArgs e)
        {
            if (string.Empty.Equals(txtMSSV.Text.Trim()))
            {
                MessageBoxEx.Show("Bạn chưa nhập MSSV", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            var sinhvien = SinhVienRepository.SelectByID(txtMSSV.Text);

            var lstDiemHocTap = DiemHocTapRepository.XemDiem(txtMSSV.Text, txtNamHoc.Text, txtHocKy.Text);

            var DataSource = new DataTable();
            DataSource.Columns.Add("NamHoc");
            DataSource.Columns.Add("HocKy");
            DataSource.Columns.Add("MaHP");
            DataSource.Columns.Add("TenHP");
            DataSource.Columns.Add("DiemQuaTrinh");
            DataSource.Columns.Add("DiemThi");
            DataSource.Columns.Add("DiemTongKet");
            DataSource.Columns.Add("DiemThiLan2");

            foreach (var item in lstDiemHocTap)
            {
                DataSource.Rows.Add(item.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.NamHoc, item.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.HocKy, item.ChiTietDangKy.ChiTietKHDT.HocPhan.MaHP, item.ChiTietDangKy.ChiTietKHDT.HocPhan.TenHP, item.DiemQuaTrinh, item.DiemThi, item.DiemTongKet, item.DiemThiLan2);
            }

            FrmInBangDiemTongHopSV frm = new FrmInBangDiemTongHopSV
            {
                Khoa = sinhvien.ChuyenNganh.Khoa.TenKhoa,
                ChuyenNganh = sinhvien.ChuyenNganh.TenCN,
                MSSV = sinhvien.MSSV,
                HoTen = sinhvien.HoTen,
                DataSource = DataSource
            };
            frm.ShowDialog();
        }

	}
}
